package com.raining.inningz.ui.play;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;


import com.raining.inningz.R;
import com.raining.inningz.baseclass.BaseFragment;
import com.raining.inningz.constants.IFragmentNameConstants;
import com.raining.inningz.constants.IParamConstants;
import com.raining.inningz.custom.validator.Form;
import com.raining.inningz.custom.validator.Validate;
import com.raining.inningz.custom.validator.customValidator.CommonErrMsgValidator;
import com.raining.inningz.custom.validator.validator.NotEmptyValidator;
import com.raining.inningz.database.dao.Matches;
import com.raining.inningz.database.dao.MatchesDao.Properties;
import com.raining.inningz.manager.NotifyManager;
import com.raining.inningz.utils.Utils;

public class PlayMatchInfoFragment extends BaseFragment implements
		OnClickListener {

	private LayoutInflater mInflater;
	private View view;

	private EditText editTxtId;
	private EditText editTxtVenue;
	private EditText editTxtMatchDate;
	private EditText editTxtVisitingTeam;
	private EditText editTxtHostingTeam;
	private EditText editTxtLeagueName;

	private PlayRulesFragment playRulesFragment;
	
	private Date date;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if(getArguments()!=null){
			
		
		matchID=getArguments().getString(IParamConstants.MATCH_ID);
		
		isEdit=getArguments().getBoolean(IParamConstants.IS_EDIT);
		}
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mInflater = inflater;

		view = inflater.inflate(R.layout.fragment_play_match_info, container,
				false);
		context = mInflater.getContext();

		initialiseUI();

		setListner();
		
		setData();

		return view;
	}
	
	private void setData() {
		
		if(!matchID.equals("")){
			List<Matches> matchesList=matchesDao.queryBuilder().where(Properties.MatchId.eq(matchID)).list();
			
			if(matchesList.size()>0){
				Matches matches=matchesList.get(0);
				
				editTxtId.setText(matches.getMatchId().toString());
				editTxtVenue.setText(matches.getVenue().toString());
				editTxtMatchDate.setText(Utils.getTimeFormDate(matches.getMatchDt()));
				
				
				editTxtVisitingTeam.setText(matches.getVisitorsShortNm().toString());
				editTxtHostingTeam.setText(matches.getHomeTeamShortNm().toString());
				editTxtLeagueName.setText(matches.getLeagueNm().toString());
				
			}
		}
		else{
			date=new Date();
			
			editTxtMatchDate.setText(Utils.getTimeFormDate(date));
		}
		
	}

	

	private void initialiseUI() {

		setHeaderIntialize(view);
		setLeftReturn();
		setLeftTextSelector(resources.getColor(R.color.top_bar_Button),
				resources.getColor(R.color.top_bar_Button_press), "InningZ");
		setRightTextSelector(resources.getColor(R.color.top_bar_Button),
				resources.getColor(R.color.top_bar_Button_press), "Save");
		setHeaderTitle(resources.getString(R.string.about), Color.BLACK);

		editTxtId = (EditText) view.findViewById(R.id.editTxtId);
		editTxtVenue = (EditText) view.findViewById(R.id.editTxtVenue);
		editTxtMatchDate = (EditText) view.findViewById(R.id.editTxtMatchDate);
		editTxtVisitingTeam = (EditText) view
				.findViewById(R.id.editTxtVisitingTeam);
		editTxtHostingTeam = (EditText) view
				.findViewById(R.id.editTxtHostingTeam);
		editTxtLeagueName = (EditText) view
				.findViewById(R.id.editTxtLeagueName);
		date=new Date();
		
	}

	private void setListner() {

		getBtnLeft().setOnClickListener(this);
		getBtnRight().setOnClickListener(this);
		editTxtMatchDate.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		if (v == getBtnLeft()) {
			fragmentManager.popBackStack();
		} else if (v == getBtnRight()) {

			
			
			Validate validateID=new Validate(editTxtId);
			validateID.addValidator(new NotEmptyValidator(context));
			
			
			/*if((!(matchesDao.queryBuilder().where(Properties.MatchId.eq(editTxtId.getText().toString())).list().size()>0)
				&&(matchID.equals("")))
				||(!(matchID.equals("")) && (matchID.equals(editTxtId.getText().toString())))
				
					
					
					
					)*/
			
			validateID.addValidator(new CommonErrMsgValidator(context, R.string.match_id_exists,(!(matchesDao.queryBuilder().where(Properties.MatchId.eq(editTxtId.getText().toString())).list().size()>0)
					&&(matchID.equals("")))
					||!(!(matchID.equals("")) && (matchID.equals(editTxtId.getText().toString())))));
			
			
			Validate venue=new Validate(editTxtVenue);
			venue.addValidator(new NotEmptyValidator(context));
			
			Validate matchDt=new Validate(editTxtMatchDate);
			matchDt.addValidator(new NotEmptyValidator(context));
			
			Validate visitorsShortNm=new Validate(editTxtVisitingTeam);
			visitorsShortNm.addValidator(new NotEmptyValidator(context));
			
			Validate homeTeamShortNm=new Validate(editTxtHostingTeam);
			homeTeamShortNm.addValidator(new NotEmptyValidator(context));
			
			Validate leagueNm=new Validate(editTxtLeagueName);
			leagueNm.addValidator(new NotEmptyValidator(context));
			
			
			Form form=new Form();
			
			form.addValidates(validateID);
			form.addValidates(venue);
			form.addValidates(matchDt);
			form.addValidates(visitorsShortNm);
			form.addValidates(homeTeamShortNm);
			form.addValidates(leagueNm);
			
			
			
			if(form.validate()){
				
			Matches matches = new Matches();

			matches.setMatchId(editTxtId.getText().toString());
			matches.setVenue(editTxtVenue.getText().toString());
			matches.setMatchDt(date);
			matches.setVisitorsShortNm(editTxtVisitingTeam.getText().toString());
			matches.setHomeTeamShortNm(editTxtHostingTeam.getText().toString());
			matches.setLeagueNm(editTxtLeagueName.getText().toString());

			long id = matchesDao.insertOrReplace(matches);
			

			
			Bundle args = new Bundle();
			args.putString(IParamConstants.MATCH_ID, matches.getMatchId());
			

			playRulesFragment = new PlayRulesFragment();
			
			playRulesFragment.setArguments(args);
			changeFragment(playRulesFragment, IFragmentNameConstants.PLAY_RULES);
			
			}


		}
		else if(v==editTxtMatchDate){
			DatePicker();
		
		}

	}

	private void DatePicker(){
		
		
		final Calendar c = Calendar.getInstance();
	int	mYear = c.get(Calendar.YEAR);
	int	mMonth = c.get(Calendar.MONTH);
	int mDay = c.get(Calendar.DAY_OF_MONTH);
	
	DatePickerDialog datePickerDialog=new DatePickerDialog(getActivity(), new OnDateSetListener() {
		
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			
			date=new Date(year, monthOfYear, dayOfMonth);	
			
			editTxtMatchDate.setText(Utils.getTimeFormDate(date));
			
			
		}
	},  mYear, mMonth, mDay);
	
	
	datePickerDialog.show();
	}
}
